package com.ruoyi.project.system.task.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.mapper.PostMapper;
import com.ruoyi.project.system.task.domain.Delay;
import com.ruoyi.project.system.task.service.DelayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/task/approve")
public class ApproveController extends BaseController {

    private String prefix = "system/task/approve";

    @Autowired
    private DelayService delayService;

    @Autowired
    private PostMapper postMapper;

    /**
     * 跳转到延期审批
     * @return
     */
    @RequiresPermissions("system:approve:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/approve";
    }


    /**
     * 修改任务
     */
    @GetMapping("/edit/{delayId}")
    public String edit(@PathVariable("delayId") Long delayId, ModelMap mmap)
    {
        mmap.put("delay", delayService.selectDelayById(delayId));
        return prefix + "/edit";
    }

    /**
     * 修改延期任务
     */
    @RequiresPermissions("system:approve:edit")
    @Log(title = "延期审核", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Delay delay)
    {
        System.out.println(delay.toString());
        delay.setApprovedTime(new Date());
        Long userId = ShiroUtils.getUserId();
        List<Post> posts = postMapper.selectPostsByUserId(userId);
        delay.setApprovedBy(posts.get(0).getPostName());
        return toAjax(delayService.updateDelayTask(delay));
    }


    /**
     * 更具条件分页查询
     * @param delay 条件
     * @return
     */
    @RequiresPermissions("system:approve:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Delay delay)
    {
        startPage();
        List<Delay> list = delayService.selectDelayList(delay);
        return getDataTable(list);
    }

    /**
     * 一键导出表
     * @param delay 查询条件
     * @return
     */
    @Log(title = "任务延期审核", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:approve:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Delay delay)
    {
        List<Delay> list = delayService.selectDelayList(delay);
        ExcelUtil<Delay> util = new ExcelUtil<Delay>(Delay.class);
        return util.exportExcel(list, "任务延期数据");
    }

    /**
     * 根据延期id查看任务信息
     * @param delayId 延期id
     * @param mmap
     * @return
     */
    @GetMapping("/show/{delayId}")
    public String show(@PathVariable("delayId") Long delayId, ModelMap mmap)
    {
        mmap.put("delay", delayService.selectDelayById(delayId));
        return prefix + "/show";
    }

}
